package net.cyue.nest.nestjava.common.util;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class JSONUtil {
  private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  static {
    OBJECT_MAPPER.setVisibility(
      PropertyAccessor.ALL,
      JsonAutoDetect.Visibility.ANY
    );
  }

  public static String toJSONString(Object obj) throws JsonProcessingException {
    return JSONUtil.OBJECT_MAPPER.writeValueAsString(obj);
  }

  public static <T> T parseObject(
        String text,
        Class<T> clz
  ) throws JsonProcessingException {
      return JSONUtil.OBJECT_MAPPER.readValue(text, clz);
  }
}
